'Copyright (C) 2009 Christiaan Cable
'
'This program is free software; you can redistribute it and/or
'modify it under the terms of the GNU General Public License
'as published by the Free Software Foundation; either version 3
'of the License, or (at your option) any later version.
'
'This program is distributed in the hope that it will be useful,
'but WITHOUT ANY WARRANTY; without even the implied warranty of
'MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
'GNU General Public License for more details (LICENCE.TXT).

Imports System.Windows.Forms

Public Class exetelwireless
    Public ISPReturnData As New ISPReturnData
    Private CookieContainer As New System.Net.CookieContainer

    Public Sub New(ByVal ConfigurationData As ConfigurationData)
        Try
            'set rollover date
            SetRolloverDate(ConfigurationData, ISPReturnData)

            Dim strHTMLData As String

            'login
            Dim URL As String = "https://www.exetel.com.au/wireless_login/"
            Dim POSTData As String = "login_name=" & ConfigurationData.Username & "&password=" & ConfigurationData.Password & "&doLogin=1&submit= Login "
            strHTMLData = ISPPost(URL, POSTData, CookieContainer, System.Net.CredentialCache.DefaultCredentials)

            If strHTMLData.Contains("The Service Number and Password you have entered is invalid") = True Then
                ISPReturnData.ErrorMessage = "Login Failed"
                Exit Sub
            End If

            'view plan
            strHTMLData = ISPGet("https://www.exetel.com.au/wireless_login/account_view_plan.php?link=link02", CookieContainer, System.Net.CredentialCache.DefaultCredentials)

            Dim ExcessCostPerGB As Single = CSng(Val(ExtractText(strHTMLData, "Excess Downloads per Gbyte:", 0, "body"">", "<")))

            'get usage
            'strHTMLData = ISPGet("https://www.exetel.com.au/wireless_login/usage_monthly_query.php", CookieContainer, System.Net.CredentialCache.DefaultCredentials)
            strHTMLData = ISPGet("https://www.exetel.com.au/wireless_login/usage_meter_v1.php?link=link06", CookieContainer, System.Net.CredentialCache.DefaultCredentials)

            If UA.DebugISP = True Then My.Computer.FileSystem.WriteAllText(GetAppDataPath() & "debug.htm", strHTMLData, False, System.Text.Encoding.ASCII)

            'logout
            ISPGet("https://www.exetel.com.au/wireless_login/logout.php", CookieContainer, System.Net.CredentialCache.DefaultCredentials)

            'process response
            ISPReturnData.PeakDownload = CSng(Val(ExtractText(strHTMLData, ">Total:<", 0, "<font color=""red""><b>", "</b>")))
            ISPReturnData.PeakUpload = CSng(Val(ExtractText(strHTMLData, ">Total:<", 232, "<b>", "</b>")))
            ISPReturnData.OffpeakDownload = CSng(Val(ExtractText(strHTMLData, ">Total:<", 148, "<b>", "</b>")))
            ISPReturnData.OffpeakUpload = CSng(Val(ExtractText(strHTMLData, ">Total:<", 0, "<b>", "</b>")))

            'ISPReturnData.LastUpdated = CDate(ExtractText(strHTMLData, "updated at", 0, " ", ")"))
            Dim LastUpdated As String = ExtractText(strHTMLData, "updated at", 0, " ", ")")
            ISPReturnData.LastUpdated = Date.ParseExact(LastUpdated, "yyyy-MM-dd HH:mm:ss", Nothing)

            '##########################################################

            ProcessISPData(ConfigurationData, ISPReturnData)

            'check for error
            If ISPReturnData.ErrorMessage <> "" Then Exit Sub

            '##########################################################

            'create graph data

            'peak
            ISPReturnData.GraphItemList.Add(New GraphItem("Peak " & Format(System.Math.Floor((ISPReturnData.PeakDownload / Val(ConfigurationData.PeakUsageLimit)) * 1000) / 10, "#0.0") & "% (" & ISPReturnData.PeakDownload & " MB)", ISPReturnData.PeakDownload, CSng(Val(ConfigurationData.PeakUsageLimit)), ISPReturnData.PeakNow))

            'offpeak
            ISPReturnData.GraphItemList.Add(New GraphItem("Offpeak Unlimited (" & ISPReturnData.OffpeakDownload & " MB)", ISPReturnData.OffpeakDownload, CSng(Val(ConfigurationData.OffpeakUsageLimit)), ISPReturnData.OffpeakNow))

            'days elapsed
            ISPReturnData.GraphItemList.Add(New GraphItem("Days Elapsed " & Format((ISPReturnData.DaysUsed / (ISPReturnData.DaysUsed + ISPReturnData.DaysLeft)) * 100, "#0") & "% (" & ISPReturnData.DaysUsed & " Days)", ISPReturnData.DaysUsed, ISPReturnData.DaysUsed + ISPReturnData.DaysLeft, False))

            '##########################################################

            'create stats data
            ISPReturnData.TextItemList.Add(New TextItem("Peak Left :", ISPReturnData.PeakUsageRemainingDescription))
            ISPReturnData.TextItemList.Add(New TextItem("Peak Avg. Left :", ISPReturnData.PeakUsageRemainingPerDayDescription))

            Dim Cost As String = "0"
            If ISPReturnData.PeakDownload > Val(ConfigurationData.PeakUsageLimit) Then
                Cost = Format(((ISPReturnData.PeakDownload - Val(ConfigurationData.PeakUsageLimit)) / 1000) * ExcessCostPerGB, "#0.00")
            End If

            ISPReturnData.TextItemList.Add(New TextItem("Excess Cost :", "$" & Cost))

            '##########################################################

            'process alerts
            ProcessAlerts(ConfigurationData, ISPReturnData)

        Catch ex As Exception
            ISPReturnData.ErrorMessage = ex.Message
        End Try
    End Sub
End Class
